package com.CST.service.impl;

import com.CST.common.api.ResultObject;
import com.CST.dto.SysRoleDTO;
import com.CST.entity.BdSysRole;
import com.CST.entity.BdSysUser;
import com.CST.mapper.cst.BdSysRoleMapper;
import com.CST.service.IBdSysRoleService;
import com.CST.service.IBdSysUserService;
import com.CST.util.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色管理 服务实现类
 * </p>
 *
 * @author y
 * @since 2021-09-27
 */
@Service
public class BdSysRoleServiceImpl extends ServiceImpl<BdSysRoleMapper, BdSysRole> implements IBdSysRoleService {
    @Autowired
    private IBdSysUserService sysUserService;

    /**
     * <p>插入新角色表</p >
     *
     * @return com.CST.common.api.ResultObject
     *
     * @author y
     * @since 2021/09/27 09:20
     */
    @Override
    public ResultObject insertSysRole(BdSysRole bdSysRole) {
        QueryWrapper<BdSysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("ROLE_NAME", bdSysRole.getRoleName());
        //判断用户是否已经存在
        List<BdSysRole> list = baseMapper.getSysRole(bdSysRole.getId(), bdSysRole.getRoleName());
        if (null != list && list.size() > 0) {
            return ResultObject.failed("角色已存在");
        }
        bdSysRole.setId(UUIDUtil.uuid());
        bdSysRole.setCreateTime(new Date());
        bdSysRole.setDelFlag("0");
        int insert = baseMapper.insert(bdSysRole);
        if (insert == 1) {
            return ResultObject.success(200, "新增成功");
        } else {
            return ResultObject.failed("新增失败");
        }

    }

    /**
     * <p>删除角色</p >
     *
     * @return com.CST.common.api.ResultObject
     *
     * @author y
     * @since 2021/09/27 09:22
     */
    @Override
    public boolean deleteSysRoleById(String id) {
        int size = sysUserService.list(Wrappers.lambdaQuery(BdSysUser.class).eq(BdSysUser::getRoleId, id)).size();
        if (size > 0) {
            return false;
        }
        BdSysRole bdSysRole = selectById(id);
        bdSysRole.setDelFlag("1");
        return updateById(bdSysRole);
    }

    /**
     * <p>通过id更新角色表</p >
     *
     * @return boolean
     *
     * @author y
     * @since 2021/09/27 09:11
     */
    @Override
    public boolean updateSysRole(BdSysRole bdSysRole) {
        bdSysRole.setUpdateTime(new Date());
        return baseMapper.updateSysRole(bdSysRole);
    }

    /**
     * <p>通过id查询角色表</p >
     *
     * @return com.CST.entity.BdSysRole
     *
     * @author y
     * @since 2021/09/27 09:13
     */
    @Override
    public BdSysRole getSysRoleById(String id) {
        return baseMapper.getSysRoleById(id);
    }


    /**
     * <p>id和名称查询</p >
     *
     * @param roleName
     * @return com.CST.entity.BdSysRole
     *
     * @author y
     * @since 2021/09/27 09:43
     */
    @Override
    public List<BdSysRole> getSysRole(String id, String roleName) {
        QueryWrapper<BdSysRole> wrapper = new QueryWrapper<>();
        wrapper.like(roleName, "roleName").or().like(id, "id");
        return baseMapper.getSysRole(id, roleName);
    }

    /**
     * <p>列表查询</p >
     *
     * @return java.util.List<com.CST.entity.BdSysRole>
     *
     * @author y
     * @since 2021/09/27 09:16
     */
    @Override
    public PageInfo<BdSysRole> getSysRolesList(SysRoleDTO sysRoleListDto) {
        PageHelper.startPage(sysRoleListDto.getPageNum(), sysRoleListDto.getPageSize());
        List<BdSysRole> sysRoles = baseMapper.getSysRolesList(sysRoleListDto);
        return new PageInfo<>(sysRoles);
    }

    /**
     * 查询全部
     *
     * @return 角色集合
     */
    @Override
    public List<BdSysRole> getSysRolesListAll() {
        return baseMapper.getSysRolesList(new SysRoleDTO());
    }

    private BdSysRole selectById(String id) {
        return baseMapper.selectById(id);
    }


}
